<?php

namespace App\Admin\Forms;

use App\Jobs\HospitalNotice;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Form;
use App\Admin\Field\IntervalWeekNum;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointIntervalNum;
use App\Models\HospitalAppointStreet;
use App\Models\Notice;
use App\Models\Street;
use Illuminate\Support\Facades\DB;

class DataUploadForm extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        try {

            return $this->response()->success('成功');

        } catch (\Exception $exception) {
            return $this->response()->error($exception->getMessage());
        }


    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $file = $this->file('file', '')
            ->accept('xlsx')
            ->maxSize(5120)
            ->disk('hospitalData')
            ->removable(false)
            ->uniqueName()
            ->move(Admin::user()->hospital_id);
        Admin::script(
            <<<JS
$('{$file->getElementClassSelector()} .file-input').on('change', function () {
    console.log('文件发生变动', this.value);
    if(this.value){
        $.ajax({
            url:'data-upload-callback',
            data:{'file':this.value},
            type:'POST',
            success:function(res){
                console.log(res);
                Dcat.success('文件上传成功，等待处理');
                setTimeout(function () {
                    Dcat.reload();
                }, 3000);
            },
            error:function(res){
                console.log(res);
                Dcat.error('文件上传失败，请重试或联系客服');

            }
        });
    }
});
JS
        );
        $this->disableSubmitButton();
        $this->disableResetButton();

    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'name' => 'John Doe',
            'email' => 'John.Doe@gmail.com',
        ];
    }
}
